local KKB_Tool_ErrorCodes = {
	"Black-Listed",
	"Not On White-List",
	"Restricted To Your Group",
	"Not On Buddy's",
	"Tool Can't Touch World",
	"Tool Can't Touch World Props",
	"Tool Can't Touch Vehicles",
	"Vehicle Doesn't Allow Tools",
	"Tool Can't Touch Weapons",
	"Weapon Doesn't Allow Tools",
	"Tool Can't Touch NPCs",
	"NPC Doesn't Allow Tools",
	"Tool Can't Touch Sents",
	"Sent Doesn't Allow Tools",
	"Tool Can't Touch Other"
}

local function KKB_CanTool_IgnoreBuddys( Group1, Group2 )
	local IgnoreBuddys = false
	if ( KKB.UserGroups[Group1].Other.IgnoreBuddys or KKB.UserGroups[Group2].Other.IgnoreBuddys ) then
		IgnoreBuddys = true
	end
	return IgnoreBuddys
end

local function KKB_BlackList_Check(Tool, Group1, Group2, Ply, Restriction)
	local BlackList = KKB.Settings.Toolguns.Blacklist
	local Restriction = KKB.ToolgunRestrictions[Tool]
	local CanTool = true
	if ( Restriction != nil ) then
		if ( BlackList ) then
			--Is On Black List
			ErrorCode = 1
			CanTool = false
		end
	else
		if ( not BlackList ) then
			--Not On White List
			ErrorCode = 2
			CanTool = false
		end
	end
	--If Tool Is Still Available
	if ( CanTool ) then
		--Check If Tool Has Players User Group
		if ( not table.HasValue(Restriction.Groups, Group1) ) then
			if ( not table.HasValue(Restriction.Groups, Group2) ) then
				--Tool Has Neither Groups
				ErrorCode = 3
				CanTool = false
			end
		end
	end
	--If Tool Is No Longer Available
	if ( not CanTool ) then
		--See If Group Can Always Use Tools
		if ( KKB.UserGroups[Group1].Other.AllToolguns or KKB.UserGroups[Group2].Other.AllToolguns ) then
			CanTool = true
		end
	end
	return CanTool
end

function KKB.CanTool( Ply, Entity, Tool )
	ErrorCode = 0
	local Restriction = KKB.ToolgunRestrictions[Tool]
	local UCL, CustomUser = KKB.GetPlayerGroups( Ply )
	--Black/White List Check
	local CanTool = KKB_BlackList_Check( Tool, UCL, CustomUser, Ply, Restriction )
	--If We Can Still use The Tool Check For Ownership
	if ( CanTool ) then
		local UniqueID = Ply:UniqueID()
		local OwnerID = Entity.KKB_OwnerID
		--Make Sure Were Checking Two Players Objects
		--Then Compare OwnerID's
		if ( OwnerID != nil and OwnerID != UniqueID ) then
			local IgnoreBuddys = KKB_CanTool_IgnoreBuddys( UCL, CustomUser )
			if ( not IgnoreBuddys ) then
				--if ( not KKB_BuddysCheck( Entity.KKB_OwnerID, UniqueID ) ) then
					ErrorCode = 4
					CanTool = false
				--end
			end
		end
		--If We Can Still Use The Tool Check For Specific Usage
		if ( CanTool ) then
			--Dealing With Something The World Owns
			if ( OwnerID == nil ) then
				if ( Entity:IsWorld() ) then
					--The Actual World
					--Can The Tool Touch World?
					ErrorCode = 5
					--If The Tool Has Never Been Set
					CanTool = false
					if ( Restriction != nil ) then
						CanTool = Restriction.Options.World
					end
					--If We Cant Use The Tool Any more
					--Check If UserGroup Can Always Touch World
					if ( not CanTool ) then
						if ( KKB.UserGroups[UCL].Toolgun.World or KKB.UserGroups[CustomUser].Toolgun.World ) then
							CanTool = true
						end
					end
				else
					--A World Entity
					--Can The Tool Touch World Props?
					ErrorCode = 6
					--If The Tool Has Never Been Set
					CanTool = false
					if ( Restriction != nil ) then
						local CanTool = Restriction.Options.WorldProps
					end
					--If We Cant Use The Tool Any more
					--Check If UserGroup Can Always Touch WorldProps
					if ( not CanTool ) then
						if ( KKB.UserGroups[UCL].Toolgun.WorldProps or KKB.UserGroups[CustomUser].Toolgun.WorldProps ) then
							CanTool = true
						end
					end
				end
			else
				--Dealing With A Player Created Entity
				local Class = Entity:GetClass()
				if ( Entity:IsVehicle() ) then
					--Can The Tool Touch Vehicles?
					ErrorCode = 7
					--If The Tool Has Never Been Set
					CanTool = false
					if ( Restriction != nil ) then
						CanTool = Restriction.Options.Vehicles
					end
					if ( CanTool ) then
						--Can The Vehicle Be Touched By Tools?
						local Restriction2 = KKB.VehicleRestrictions[Class]
						--Make Sure The vehicle Has Had Options Set
						if ( Restrictions2 != nil ) then
							ErrorCode = 8
							CanTool = Restriction2.Options.Toolgun
						end
					end
					--If We Cant Use The Tool Any more
					if ( not CanTool ) then
						--Can The Group Always Touch Vehicles?
						if ( KKB.UserGroups[UCL].Toolgun.Vehicles or KKB.UserGroups[CustomUser].Toolgun.Vehicles ) then
							CanTool = true
						end
					end
				elseif ( Entity:IsWeapon() ) then
					--Can The Tool Touch Weapons?
					ErrorCode = 9
					--If The Tool Has Never Been Set
					CanTool = false
					if ( Restriction != nil ) then
						CanTool = Restriction.Options.Weapons
					end
					if ( CanTool ) then
						--Can The Weapon Be Touched By Tools?
						local Restriction2 = KKB.WeaponRestrictions[Class]
						--Make Sure The Weapon Has Had Options Set
						if ( Restrictions2 != nil ) then
							ErrorCode = 10
							CanTool = Restriction2.Options.Toolgun
						end
					end
					--If We Cant Use The Tool Any more
					if ( not CanTool ) then
						--Can The Group Always Touch Weapons?
						if ( KKB.UserGroups[UCL].Toolgun.Weapons or KKB.UserGroups[CustomUser].Toolgun.Weapons ) then
							CanTool = true
						end
					end
				elseif ( Entity:IsNPC() ) then
					--Can The Tool Touch NPCs?
					ErrorCode = 11
					--If The Tool Has Never Been Set
					CanTool = false
					if ( Restriction != nil ) then
						CanTool = Restriction.Options.NPCs
					end
					if ( CanTool ) then
						--Can The NPC Be Touched By Tools?
						local Restriction2 = KKB.NPCRestrictions[Class]
						--Make Sure The NPC Has Had Options Set
						if ( Restrictions2 != nil ) then
							ErrorCode = 12
							CanTool = Restriction2.Options.Toolgun
						end
					end
					--If We Cant Use The Tool Any more
					if ( not CanTool ) then
						--Can The Group Always Touch NPCs?
						if ( KKB.UserGroups[UCL].Toolgun.NPCs or KKB.UserGroups[CustomUser].Toolgun.NPCs ) then
							CanTool = true
						end
					end
				else
					--Can The Tool Touch Entitys?
					ErrorCode = 13
					--If The Tool Has Never Been Set
					CanTool = false
					if ( Restriction != nil ) then
						CanTool = Restriction.Options.Entitys
					end
					if ( CanTool ) then
						--Can The Entity Be Touched By Tools?
						local Restriction2 = KKB.EntityRestrictions[Class]
						--Make Sure The Entity Has Had Options Set
						if ( Restrictions2 != nil ) then
							ErrorCode = 14
							CanTool = Restriction2.Options.Toolgun
							--If We Cant Use The Tool Any more
							if ( not CanTool ) then
								--Can The Group Always Touch Entitys?
								if ( KKB.UserGroups[UCL].Toolgun.Entitys or KKB.UserGroups[CustomUser].Toolgun.Entitys ) then
									CanTool = true
								end
							end
						end
					end
					--I'm Not Quite Sure I Made An 'Other' Check
					--It's Quite Redundant But There May Be A Purpose
					--The Other Errors Are Going To Pop Up If
					--Its An Entity That's Never Been Set
					--I Suppose This Will Cause A Little Confusion
					--But I Don't See An Easy Way Around It
					--As If Any Of This Was Easy
					--Insert Eye Roll
					if ( Restriction2 == nil ) then
						if ( not CanTool ) then
							--Can The Tool Touch Other?
							ErrorCode = 15
							--If The Tool Has Never Been Set
							CanTool = false
							if ( Restriction != nil ) then
								CanTool = Restriction.Options.Other
							end
							if ( not CanTool ) then
								--Can The Group Always Touch Other?
								if ( KKB.UserGroups[UCL].Toolgun.Other or KKB.UserGroups[CustomUser].Toolgun.Other ) then
									CanTool = true
								end
							end
						end
					end
				end
			end
		end
	end
	--Make Sure We Only Ever Return False
	if ( not CanTool ) then
		Ply:SendLua("GAMEMODE:AddNotify(\""..KKB_Tool_ErrorCodes[ErrorCode].."\", NOTIFY_ERROR, 10)")
		return CanTool
	end
end

local function KKB_CanTool( Ply, Trace, Tool )
	local Entity = Trace.Entity
	return KKB.CanTool( Ply, Entity, Tool )
end
hook.Add("CanTool", "KKB_CanTool", KKB_CanTool)